<?php
namespace app\bis\controller;
use think\Controller;

class Deal extends Base
{
    /**
     * @return mixed 商户中心的 deal列表页面 小伙伴自行完成
     */
    public function index()
    {
        $bisId = $this->getLoginUser()->bis_id;
        $deals=model('Deal')->getNormalDealsByBisIdStatus($bisId,['>',-1]);
        return $this->fetch('',['deals'=>$deals]);
    }

    public function  add() {
        $bisId = $this->getLoginUser()->bis_id;
        if(request()->isPost()) {
            // 走插入逻辑
            $data = input('post.');
            // 严格校验提交的数据， tp5 validate 小伙伴自行完成，
            $validate = validate('Deal');
            if(!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            }

            $location = model('BisLocation')->get($data['location_ids'][0]);
            $deals = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'image' => $data['image'],
                'category_id' => $data['category_id'],
                'se_category_id' => empty($data['se_category_id']) ? '' : implode(',', $data['se_category_id']),
                'city_id' => $data['city_id'],
                'se_city_id' => $data['se_city_id'],
                'location_ids' => empty($data['location_ids']) ? '' : implode(',', $data['location_ids']),
                'start_time' => strtotime($data['start_time']),
                'end_time' => strtotime($data['end_time']),
                'total_count' => $data['total_count'],
                'origin_price' => $data['origin_price'],
                'current_price' => $data['current_price'],
                'coupons_begin_time' => strtotime($data['coupons_begin_time']),
                'coupons_end_time' => strtotime($data['coupons_end_time']),
                'notes' => $data['notes'],
                'description' => $data['description'],
                'bis_account_id' => $this->getLoginUser()->id,
                'xpoint' => $location->xpoint,
                'ypoint' => $location->ypoint,
                'status' => 0,
            ];
            if (!empty($data['id'])){
                return $this->update($deals,$data['id']);
            }
            $id = model('Deal')->add($deals);
            if($id) {
                $this->success('添加成功', url('deal/index'));
            }else {
                $this->error('添加失败');
            }

        }else {
            //获取一级城市的数据
            $citys = model('City')->getNormalCitysByParentId();
            //获取一级栏目的数据
            $categorys = model('Category')->getNormalCategoryByParentId();
            return $this->fetch('', [
                'citys' => $citys,
                'categorys' => $categorys,
                'bislocations' => model('BisLocation')->getNormalLocationByBisId($bisId),
            ]);
        }
    }

    public function edit($id)
    {
        $bisId = $this->getLoginUser()->bis_id;
        $deal=model('Deal')->get($id);
        $citys = model('City')->getNormalCitysByParentId();
        $categorys = model('Category')->getNormalCategoryByParentId();
        $bislocations = model('BisLocation')->getNormalLocationByBisId($bisId);
        $secategorys = model('Category')->getNormalCategoryByParentId($deal['category_id']);
        $catchecked=getSeCategoryBySeCategoryId($deal['se_category_id']);
        $locationchecked=getLocationByLocationIds($deal['location_ids']);
        return $this->fetch('',[
            'deal'=>$deal,
            'citys' => $citys,
            'categorys' => $categorys,
            'bislocations' => $bislocations,
            'secategorys' => $secategorys,
            'catchecked' =>$catchecked,
            'locationchecked' =>$locationchecked
            ]);
    }

    public function update($data,$id)
    {
        $res=model('Deal')->save($data,['id' =>intval($id)]);
        if($res){
            $this->success('更新成功',url('deal/index'));
        }else{
            $this->error('更新失败');
        }
    }

    public function delist()
    {
        $bisId = $this->getLoginUser()->bis_id;
        $deals=model('Deal')->getNormalDealsByBisIdStatus($bisId,-1);
        return $this->fetch('',['deals'=>$deals]);
    }
}